import request from "@/utils/request";
import { Local } from "@/utils/storage";
import { Notify } from "vant";
//返回结果的时候记得异步获取，调用函数也要异步
const personContacts='/person-contacts';
export async function getAddFriend() {
    try {
        const res = await request.get(personContacts+'/show-add');
        Local.set('addList', res.data);
        if (res.data === null) return 0;
        return res.data.length;
    } catch (error) {
        console.log(error);
        throw error; // 重新抛出错误
    }
}
export async function getFriendList() {
    try {
        const res = await request.get(personContacts+'/show-all');
        return res.data// 返回 res.data.length
    } catch (error) {
        console.log(error);
        throw error; // 重新抛出错误
    }
}
export async function searchFriend(searchInfo) {
    try {
        const res = await request.get(personContacts+'/search', {
            params: {
                username: searchInfo
            }
        });
        return res.data;
    } catch (error) {
        console.log(error);
        throw error; // 重新抛出错误
    }
}
export function addFriend(addInfo){
    request.post(personContacts+'/add',addInfo).then(res=>{
        if(res.code===2000){
            Notify({ type: 'success', message: res.data });
        }
        else {
            Notify({ type: 'danger', message: res.data });
        }
    }).catch(error => {
        return error;
    });
}
export function acceptAdd(addId,nickname){
    request.get(personContacts+'/accept',{
        params:{
            addId:addId,
            nickname:nickname
        }
    }).then(res=>{
        if(res.code===2000){
            Notify({ type: 'success', message: res.data });
        }
        else {
            Notify({ type: 'danger', message: res.data });
        }
    }).catch(error => {
        return error;
    });
}
export function refuseAdd(addId){
    request.get(personContacts+'/refuse',{
        params:{
            addId:addId
        }
    }).then(res=>{
        if(res.code===2000){
            Notify({ type: 'success', message: res.data });
        }
        else {
            Notify({ type: 'danger', message: res.data });
        }
    }).catch(error => {
        return error;
    });
}
export function deleteFriend(friendId){
    request.get(personContacts+'/delete',{
        params:{
            friendId:friendId
        }
    }).then(res=>{
        if(res.code===2000){
            Notify({ type: 'success', message: res.data });
        }
        else {
            Notify({ type: 'danger', message: res.data });
        }
    }).catch(error => {
        return error;
    });
}
export function resetNickname(friendId,nickname){
    request.get(personContacts+'/reset',{
        params:{
            friendId:friendId,
            nickname:nickname
        }
    }).then(res=>{
        if(res.code===2000){
            Notify({ type: 'success', message: res.data });
        }
        else {
            Notify({ type: 'danger', message: res.data });
        }
    }).catch(error => {
        return error;
    });
}